<!DOCTYPE html>
<html lang="zh_cn">
<head>
          <title>来玩魔王的咚</title>
        <meta name="viewport" content="width=device-width, initial-scale=1" />
        <meta charset="utf-8" />
        <!-- twitter card metadata -->
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="/images/mowang.png">
<meta name="twitter:site" content="">
<meta name="twitter:title" content="Ansible 安装部署">
<meta name="twitter:description" content="<p>Ansible的2种安装的方法：pip安装，yum安装；在Azure中使用Ansible。</p>">
        <!-- OG Tags -->
<meta property="og:url" content="/installing-ansible.html"/>
<meta property="og:title" content="来玩魔王的咚 | Ansible 安装部署" />
<meta property="og:description" content="<p>Ansible的2种安装的方法：pip安装，yum安装；在Azure中使用Ansible。</p>" />
        <!-- favicon -->
        <link rel="icon" type="image/png" href="/images/mowang.png">
        <!-- moment.js for date formatting -->
        <script src="/theme/js/moment.js"></script>
        <!-- css -->
        <link rel="stylesheet" type="text/css" href="/theme/css/main.css" />
        <!-- 左边的menu，如果页面高度不够，就跟着滚动，否则文章分类显示不全 -->
        <link rel="stylesheet" type="text/css" href="/theme/css/mycss/menu.css" />
		<script>
			
                /*! grunt-grunticon Stylesheet Loader - v2.1.2 | https://github.com/filamentgroup/grunticon | (c) 2015 Scott Jehl, Filament Group, Inc. | MIT license. */
    
    (function(e){function t(t,n,r,o){"use strict";function a(){for(var e,n=0;u.length>n;n++)u[n].href&&u[n].href.indexOf(t)>-1&&(e=!0);e?i.media=r||"all":setTimeout(a)}var i=e.document.createElement("link"),l=n||e.document.getElementsByTagName("script")[0],u=e.document.styleSheets;return i.rel="stylesheet",i.href=t,i.media="only x",i.onload=o||null,l.parentNode.insertBefore(i,l),a(),i}var n=function(r,o){"use strict";if(r&&3===r.length){var a=e.navigator,i=e.Image,l=!(!document.createElementNS||!document.createElementNS("http://www.w3.org/2000/svg","svg").createSVGRect||!document.implementation.hasFeature("http://www.w3.org/TR/SVG11/feature#Image","1.1")||e.opera&&-1===a.userAgent.indexOf("Chrome")||-1!==a.userAgent.indexOf("Series40")),u=new i;u.onerror=function(){n.method="png",n.href=r[2],t(r[2])},u.onload=function(){var e=1===u.width&&1===u.height,a=r[e&&l?0:e?1:2];n.method=e&&l?"svg":e?"datapng":"png",n.href=a,t(a,null,null,o)},u.src="",document.documentElement.className+=" grunticon"}};n.loadCSS=t,e.grunticon=n})(this);(function(e,t){"use strict";var n=t.document,r="grunticon:",o=function(e){if(n.attachEvent?"complete"===n.readyState:"loading"!==n.readyState)e();else{var t=!1;n.addEventListener("readystatechange",function(){t||(t=!0,e())},!1)}},a=function(e){return t.document.querySelector('link[href$="'+e+'"]')},c=function(e){var t,n,o,a,c,i,u={};if(t=e.sheet,!t)return u;n=t.cssRules?t.cssRules:t.rules;for(var l=0;n.length>l;l++)o=n[l].cssText,a=r+n[l].selectorText,c=o.split(");")[0].match(/US\-ASCII\,([^"']+)/),c&&c[1]&&(i=decodeURIComponent(c[1]),u[a]=i);return u},i=function(e){var t,o,a;o="data-grunticon-embed";for(var c in e)if(a=c.slice(r.length),t=n.querySelectorAll(a+"["+o+"]"),t.length)for(var i=0;t.length>i;i++)t[i].innerHTML=e[c],t[i].style.backgroundImage="none",t[i].removeAttribute(o);return t},u=function(t){"svg"===e.method&&o(function(){i(c(a(e.href))),"function"==typeof t&&t()})};e.embedIcons=i,e.getCSS=a,e.getIcons=c,e.ready=o,e.svgLoadedCallback=u,e.embedSVG=u})(grunticon,this);
                
                grunticon(["/theme/css/icons.data.svg.css", "/theme/css/icons.data.png.css", "/theme/css/icons.fallback.css"]);
            </script>
        <noscript><link href="/theme/css/icons.fallback.css" rel="stylesheet"></noscript>
        <!-- menu toggle javascript -->
        <script type="text/javascript">
            document.addEventListener("DOMContentLoaded", initMenu);
            
            function initMenu(){
                var menu = document.getElementById("menu");
                var menulink = document.getElementById("menu-link");
                menulink.addEventListener("click", function toggleMenu(){
                        window.event.preventDefault();
                        menulink.classList.toggle('active');
                        menu.classList.toggle('active');              
                    });
            };
        </script>
        <!-- 不蒜子 -->
        <script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>

    <meta name="description" content="<p>Ansible的2种安装的方法：pip安装，yum安装；在Azure中使用Ansible。</p>" />

    <meta name="tags" content="ansible" />
    <meta name="tags" content="azure" />
  <!-- 替换部分base的样式，看文章时，要再宽一点，右边有很多空间可以撑开 -->
  <link rel="stylesheet" type="text/css" href="/theme/css/mycss/article.css" />

</head>
<body>
    <div role="banner" id="masthead">
        <header>
            <a href="/"><img src="/images/mowang.png" alt="McManus Logo"></a>
                <h1>来玩魔王的咚@骑士救兵</h1>
            <a href="#menu" id="menu-link">more stuff</a>
            <nav id="menu">
                <ul>
                        <li><a href="/tags">tags</a></li>
                            <li><a href="/category/cloud.html">Cloud</a></li>
                            <li><a href="/category/docker.html">Docker</a></li>
                            <li><a href="/category/go.html">Go</a></li>
                            <li><a href="/category/linux.html">Linux</a></li>
                            <li><a href="/category/python.html">Python</a></li>
                            <li><a href="/category/xue-xi-bi-ji.html">学习笔记</a></li>
                            <li class="active"><a href="/category/yun-wei-zi-dong-hua.html">运维自动化</a></li>
                </ul>
            </nav>
        </header>
    </div>
        <div class="page" role="main">
  <div class="article" role="article">
    <article>
        <footer>
            <a name="top"></a>
            <p>
              <time datetime=" 2018-10-12 09:43:51+08:00">
                <script>document.write(moment('2018-10-12 09:43:51+08:00').format('LL'));</script>
              </time>
              ~
              <time datetime=" 2018-10-12 09:43:51+08:00">
                <script>document.write(moment('2021-06-17 17:30:00+08:00').format('LL'));</script>
              </time>
            </p>
        </footer>
        <header>
          <h2>
            Ansible 安装部署
          </h2>
        </header>
      <div class="content">
         <div class="toc">
<ul>
<li><a href="#jian-jie">简介</a></li>
<li><a href="#an-zhuang-bu-shu">安装部署</a><ul>
<li><a href="#pip-fang-shi">PIP方式</a></li>
<li><a href="#config-file-wen-jian">config file 文件</a></li>
<li><a href="#yum-fang-shi">YUM方式</a></li>
</ul>
</li>
<li><a href="#zai-azure-zhong-shi-yong-ansible">在 Azure 中使用 Ansible</a><ul>
<li><a href="#azure-ping-ju">Azure 凭据</a></li>
<li><a href="#shi-li-chuang-jian-zi-yuan-zu">示例：创建资源组</a></li>
</ul>
</li>
</ul>
</div>
<h3 id="jian-jie"><a class="toclink" href="#jian-jie">简介</a></h3>
<p>同类自动化工具有一下这些（排前面的越热门）：</p>
<ul>
<li>Ansible</li>
<li>SaltStack</li>
<li>Puppet</li>
<li>Chef</li>
<li>Fabric</li>
</ul>
<p>Ansible的通信方式：</p>
<ul>
<li>Paramiko模块：1.3版本之前，最早的连接模块</li>
<li>OpenSSH：1.3版本开始默认使用的方式，0.5版本起就有了，逐步替代了paramiko</li>
<li>加速模式：对Ad-Hoc命令不友好，但Playbook的能提高性能。速度是paramiko的10倍，SSH的2~6倍</li>
<li>Pipelining：1.5+支持，且为官方推荐的加速模式</li>
</ul>
<h3 id="an-zhuang-bu-shu"><a class="toclink" href="#an-zhuang-bu-shu">安装部署</a></h3>
<p>先看下面的报错信息：</p>
<div class="highlight"><pre><span></span><code><span class="gp">[root@Ansible ~]# </span>ansible --version
<span class="go">ERROR: Ansible requires a minimum of Python2 version 2.6 or Python3 version 3.5. Current version: 3.4.9 (default, Aug 14 2018, 21:28:57) [GCC ④.8.5 20150623 (Red Hat 4.8.5-28)]</span>
</code></pre></div>

<p>当时安装的是 ansible 2.6.4 的版本：</p>
<div class="highlight"><pre><span></span><code><span class="gp">[root@Ansible ~]# </span>pip freeze <span class="p">|</span> grep ansible
<span class="go">ansible==2.6.4</span>
</code></pre></div>

<h4 id="pip-fang-shi"><a class="toclink" href="#pip-fang-shi">PIP方式</a></h4>
<p>推荐使用pip方式安装。<br>
用pip安装的一个好处是，可以使用你当前pip命令的python环境来安装ansible，而不是系统默认的python环境。如果系统有多个python环境的话。<br>
安装 python3 和 pip 可以看这里：<a href="https://blog.51cto.com/steed/2288045">https://blog.51cto.com/steed/2288045</a><br>
通过 Virtualenv 实现 python 多环境扩展可以看这里：<a href="https://blog.51cto.com/steed/2298681">https://blog.51cto.com/steed/2298681</a><br>
下面的命令，如果使用默认的源太慢，还可以指定第三方源来安装：</p>
<div class="highlight"><pre><span></span><code>pip install -i https://mirrors.163.com/pypi/simple ansible
</code></pre></div>

<p>下面是pip安装和验证的全过程，命令就一句 <code>pip install ansible</code> ，不过前提是安装好 python 和 pip ：</p>
<div class="highlight"><pre><span></span><code><span class="gp gp-VirtualEnv">(venv)</span> <span class="gp">[root@Python3 ~]# </span>pip install ansible
<span class="go">Collecting ansible</span>
<span class="go">  Downloading https://files.pythonhosted.org/packages/7c/4f/a1843687642b8e33637e312b9fb17ca7f68be0641131a92d883368dc6d1c/ansible-2.7.0.tar.gz (11.8MB)</span>
<span class="go">    100% |████████████████████████████████| 11.8MB 37kB/s </span>
<span class="go">Collecting jinja2 (from ansible)</span>
<span class="go">  Downloading https://files.pythonhosted.org/packages/7f/ff/ae64bacdfc95f27a016a7bed8e8686763ba4d277a78ca76f32659220a731/Jinja2-2.10-py2.py3-none-any.whl (126kB)</span>
<span class="go">    100% |████████████████████████████████| 133kB 77kB/s </span>
<span class="go">Collecting PyYAML (from ansible)</span>
<span class="go">  Downloading https://files.pythonhosted.org/packages/9e/a3/1d13970c3f36777c583f136c136f804d70f500168edc1edea6daa7200769/PyYAML-3.13.tar.gz (270kB)</span>
<span class="go">    100% |████████████████████████████████| 276kB 61kB/s </span>
<span class="go">Collecting paramiko (from ansible)</span>
<span class="go">  Downloading https://files.pythonhosted.org/packages/cf/ae/94e70d49044ccc234bfdba20114fa947d7ba6eb68a2e452d89b920e62227/paramiko-2.4.2-py2.py3-none-any.whl (193kB)</span>
<span class="go">    100% |████████████████████████████████| 194kB 52kB/s </span>
<span class="go">Collecting cryptography (from ansible)</span>
<span class="go">  Downloading https://files.pythonhosted.org/packages/59/32/92cade62c645756a83598edf56289e9b19aae5370642a7ce690cd06bc72f/cryptography-2.3.1-cp34-abi3-manylinux1_x86_64.whl (2.1MB)</span>
<span class="go">    100% |████████████████████████████████| 2.1MB 35kB/s </span>
<span class="go">Requirement already satisfied: setuptools in /opt/ansible/venv/lib/python3.6/site-packages (from ansible) (40.4.3)</span>
<span class="go">Collecting MarkupSafe&gt;=0.23 (from jinja2-&gt;ansible)</span>
<span class="go">  Downloading https://files.pythonhosted.org/packages/4d/de/32d741db316d8fdb7680822dd37001ef7a448255de9699ab4bfcbdf4172b/MarkupSafe-1.0.tar.gz</span>
<span class="go">Collecting pynacl&gt;=1.0.1 (from paramiko-&gt;ansible)</span>
<span class="go">  Downloading https://files.pythonhosted.org/packages/27/15/2cd0a203f318c2240b42cd9dd13c931ddd61067809fee3479f44f086103e/PyNaCl-1.3.0-cp34-abi3-manylinux1_x86_64.whl (759kB)</span>
<span class="go">    100% |████████████████████████████████| 768kB 33kB/s </span>
<span class="go">Collecting bcrypt&gt;=3.1.3 (from paramiko-&gt;ansible)</span>
<span class="go">  Downloading https://files.pythonhosted.org/packages/b8/09/905ec939994e2c49dcffff72f823802557f166b3815ea54c1db3671eed42/bcrypt-3.1.4-cp36-cp36m-manylinux1_x86_64.whl (54kB)</span>
<span class="go">    100% |████████████████████████████████| 61kB 38kB/s </span>
<span class="go">Collecting pyasn1&gt;=0.1.7 (from paramiko-&gt;ansible)</span>
<span class="go">  Downloading https://files.pythonhosted.org/packages/d1/a1/7790cc85db38daa874f6a2e6308131b9953feb1367f2ae2d1123bb93a9f5/pyasn1-0.4.4-py2.py3-none-any.whl (72kB)</span>
<span class="go">    100% |████████████████████████████████| 81kB 26kB/s </span>
<span class="go">Collecting six&gt;=1.4.1 (from cryptography-&gt;ansible)</span>
<span class="go">  Downloading https://files.pythonhosted.org/packages/67/4b/141a581104b1f6397bfa78ac9d43d8ad29a7ca43ea90a2d863fe3056e86a/six-1.11.0-py2.py3-none-any.whl</span>
<span class="go">Collecting cffi!=1.11.3,&gt;=1.7 (from cryptography-&gt;ansible)</span>
<span class="go">  Downloading https://files.pythonhosted.org/packages/6d/c0/47db8f624f3e4e2f3f27be03a93379d1ba16a1450a7b1aacfa0366e2c0dd/cffi-1.11.5-cp36-cp36m-manylinux1_x86_64.whl (421kB)</span>
<span class="go">    100% |████████████████████████████████| 430kB 103kB/s </span>
<span class="go">Collecting asn1crypto&gt;=0.21.0 (from cryptography-&gt;ansible)</span>
<span class="go">  Downloading https://files.pythonhosted.org/packages/ea/cd/35485615f45f30a510576f1a56d1e0a7ad7bd8ab5ed7cdc600ef7cd06222/asn1crypto-0.24.0-py2.py3-none-any.whl (101kB)</span>
<span class="go">    100% |████████████████████████████████| 102kB 79kB/s </span>
<span class="go">Collecting idna&gt;=2.1 (from cryptography-&gt;ansible)</span>
<span class="go">  Downloading https://files.pythonhosted.org/packages/4b/2a/0276479a4b3caeb8a8c1af2f8e4355746a97fab05a372e4a2c6a6b876165/idna-2.7-py2.py3-none-any.whl (58kB)</span>
<span class="go">    100% |████████████████████████████████| 61kB 59kB/s </span>
<span class="go">Collecting pycparser (from cffi!=1.11.3,&gt;=1.7-&gt;cryptography-&gt;ansible)</span>
<span class="go">  Downloading https://files.pythonhosted.org/packages/68/9e/49196946aee219aead1290e00d1e7fdeab8567783e83e1b9ab5585e6206a/pycparser-2.19.tar.gz (158kB)</span>
<span class="go">    100% |████████████████████████████████| 163kB 111kB/s </span>
<span class="go">Building wheels for collected packages: ansible, PyYAML, MarkupSafe, pycparser</span>
<span class="go">  Running setup.py bdist_wheel for ansible ... done</span>
<span class="go">  Stored in directory: /root/.cache/pip/wheels/4d/9b/69/922392aa2f3189d99e672afbd178887927ce5420e2cc874dcd</span>
<span class="go">  Running setup.py bdist_wheel for PyYAML ... done</span>
<span class="go">  Stored in directory: /root/.cache/pip/wheels/ad/da/0c/74eb680767247273e2cf2723482cb9c924fe70af57c334513f</span>
<span class="go">  Running setup.py bdist_wheel for MarkupSafe ... done</span>
<span class="go">  Stored in directory: /root/.cache/pip/wheels/33/56/20/ebe49a5c612fffe1c5a632146b16596f9e64676768661e4e46</span>
<span class="go">  Running setup.py bdist_wheel for pycparser ... done</span>
<span class="go">  Stored in directory: /root/.cache/pip/wheels/f2/9a/90/de94f8556265ddc9d9c8b271b0f63e57b26fb1d67a45564511</span>
<span class="go">Successfully built ansible PyYAML MarkupSafe pycparser</span>
<span class="go">Installing collected packages: MarkupSafe, jinja2, PyYAML, pycparser, cffi, six, pynacl, asn1crypto, idna, cryptography, bcrypt, pyasn1, paramiko, ansible</span>
<span class="go">Successfully installed MarkupSafe-1.0 PyYAML-3.13 ansible-2.7.0 asn1crypto-0.24.0 bcrypt-3.1.4 cffi-1.11.5 cryptography-2.3.1 idna-2.7 jinja2-2.10 paramiko-2.4.2 pyasn1-0.4.4 pycparser-2.19 pynacl-1.3.0 six-1.11.0</span>
<span class="gp gp-VirtualEnv">(venv)</span> <span class="gp">[root@Python3 ~]# </span>ansible --version
<span class="go">ansible 2.7.0</span>
<span class="go">  config file = None</span>
<span class="go">  configured module search path = [&#39;/root/.ansible/plugins/modules&#39;, &#39;/usr/share/ansible/plugins/modules&#39;]</span>
<span class="go">  ansible python module location = /opt/ansible/venv/lib/python3.6/site-packages/ansible</span>
<span class="go">  executable location = /opt/ansible/venv/bin/ansible</span>
<span class="go">  python version = 3.6.6 (default, Aug 13 2018, 18:24:23) [GCC ④.8.5 20150623 (Red Hat 4.8.5-28)]</span>
<span class="gp gp-VirtualEnv">(venv)</span> <span class="gp">[root@Python3 ~]#</span>
</code></pre></div>

<h4 id="config-file-wen-jian"><a class="toclink" href="#config-file-wen-jian">config file 文件</a></h4>
<p>pip安装是没有config file文件的，上面的命令显示是None。我们可以去官网下载一份文件。<br>
官方文档：<a href="https://docs.ansible.com/ansible/latest/installation_guide/intro_configuration.html">https://docs.ansible.com/ansible/latest/installation_guide/intro_configuration.html</a>  </p>
<blockquote>
<p>If you installed Ansible from pip or from source, you may want to create this file in order to override default settings in Ansible.</p>
</blockquote>
<p>文档里给了一个配置文件在GitHub上的连接，下载下来：</p>
<div class="highlight"><pre><span></span><code><span class="gp">[root@Python36 ~]# </span>mkdir /etc/ansible
<span class="gp">[root@Python36 ~]# </span><span class="nb">cd</span> /etc/ansible
<span class="gp">[root@Python36 ansible]# </span>wget https://raw.githubusercontent.com/ansible/ansible/devel/examples/ansible.cfg
</code></pre></div>

<p>之后ansible会自动搜索，无需操作。再执行一遍命令，config file文件就有了。  </p>
<h4 id="yum-fang-shi"><a class="toclink" href="#yum-fang-shi">YUM方式</a></h4>
<p>通过yum安装ansible，不过不推荐。  </p>
<h5 id="yum-yuan"><a class="toclink" href="#yum-yuan">yum源</a></h5>
<p>我用的是Centos7的系统，国外的源可能连不上，不过肯定慢，先换成国内的源。<br>
yum源文件的位置在：/etc/yum.repos.d/ 把里面的文件都干掉。可以删，不过不是好习惯。在文件夹下再创建个bak文件夹，然后把所有文件都移进去。<br>
用下面的命令可以把阿里的源下载到本地：</p>
<div class="highlight"><pre><span></span><code><span class="gp">[root@Ansible yum.repos.d]# </span>curl -O https://mirrors.aliyun.com/repo/Centos-7.repo
<span class="gp">[root@Ansible yum.repos.d]# </span>curl -O https://mirrors.aliyun.com/repo/epel-7.repo
</code></pre></div>

<p>然后可以用makecache把软件包的信息缓存到本地，顺便可以验证一下yum源：</p>
<div class="highlight"><pre><span></span><code><span class="gp">[root@Ansible yum.repos.d]# </span>yum makecache
<span class="go">已加载插件：fastestmirror</span>
<span class="go">Loading mirror speeds from cached hostfile</span>
<span class="go"> * base: mirrors.aliyun.com</span>
<span class="go"> * extras: mirrors.aliyun.com</span>
<span class="go"> * updates: mirrors.aliyun.com</span>
<span class="go">base                                                     | 3.6 kB     00:00     </span>
<span class="go">epel                                                     | 3.2 kB     00:00     </span>
<span class="go">extras                                                   | 3.4 kB     00:00     </span>
<span class="go">updates                                                  | 3.4 kB     00:00     </span>
<span class="gp gp-VirtualEnv">(1/11)</span><span class="go">: epel/x86_64/prestodelta                            |  465 B   00:00     </span>
<span class="gp gp-VirtualEnv">(2/11)</span><span class="go">: extras/7/x86_64/filelists_db                       | 596 kB   00:09     </span>
<span class="gp gp-VirtualEnv">(3/11)</span><span class="go">: epel/x86_64/other                                  | 2.2 MB   00:09     </span>
<span class="gp gp-VirtualEnv">(4/11)</span><span class="go">: extras/7/x86_64/other_db                           | 120 kB   00:00     </span>
<span class="gp gp-VirtualEnv">(5/11)</span><span class="go">: base/7/x86_64/other_db                             | 2.5 MB   00:11     </span>
<span class="gp gp-VirtualEnv">(6/11)</span><span class="go">: extras/7/x86_64/prestodelta                        |  84 kB   00:01     </span>
<span class="gp gp-VirtualEnv">(7/11)</span><span class="go">: updates/7/x86_64/prestodelta                       | 540 kB   00:02     </span>
<span class="gp gp-VirtualEnv">(8/11)</span><span class="go">: updates/7/x86_64/other_db                          | 483 kB   00:01     </span>
<span class="gp gp-VirtualEnv">(9/11)</span><span class="go">: updates/7/x86_64/filelists_db                      | 2.9 MB   00:09     </span>
<span class="gp gp-VirtualEnv">(10/11)</span><span class="go">: base/7/x86_64/filelists_db                        | 6.9 MB   00:32     </span>
<span class="gp gp-VirtualEnv">(11/11)</span><span class="go">: epel/x86_64/filelists                             |  10 MB   00:38     </span>
<span class="go">epel                                                                12687/12687</span>
<span class="go">epel                                                                12687/12687</span>
<span class="go">元数据缓存已建立</span>
</code></pre></div>

<h5 id="jin-xing-yum-an-zhuang"><a class="toclink" href="#jin-xing-yum-an-zhuang">进行yum安装</a></h5>
<p>yum安装比较省心，依赖都自动解决的，到这应该就没什么难度了：</p>
<div class="highlight"><pre><span></span><code>yum install ansible
</code></pre></div>

<h5 id="yan-zheng"><a class="toclink" href="#yan-zheng">验证</a></h5>
<p>执行ansible --version 查看版本，顺便验证一下安装：</p>
<div class="highlight"><pre><span></span><code><span class="gp">[root@Ansible ~]# </span>ansible --version
<span class="go">ansible 2.6.4</span>
<span class="go">  config file = /etc/ansible/ansible.cfg</span>
<span class="go">  configured module search path = [u&#39;/root/.ansible/plugins/modules&#39;, u&#39;/usr/share/ansible/plugins/modules&#39;]</span>
<span class="go">  ansible python module location = /usr/lib/python2.7/site-packages/ansible</span>
<span class="go">  executable location = /usr/bin/ansible</span>
<span class="go">  python version = 2.7.5 (default, Apr 11 2018, 07:36:10) [GCC ④.8.5 20150623 (Red Hat 4.8.5-28)]</span>
</code></pre></div>

<h3 id="zai-azure-zhong-shi-yong-ansible"><a class="toclink" href="#zai-azure-zhong-shi-yong-ansible">在 Azure 中使用 Ansible</a></h3>
<p>要在azure上使用，需要一些额外的安装步骤。具体可以参考Azure的官方文档：<br>
<a href="https://docs.microsoft.com/zh-cn/azure/developer/ansible/install-on-linux-vm?tabs=ansible#install-ansible-on-the-virtual-machine">https://docs.microsoft.com/zh-cn/azure/developer/ansible/install-on-linux-vm?tabs=ansible#install-ansible-on-the-virtual-machine</a>  </p>
<p>pip安装ansible模块的时候，加一个额外的参数：</p>
<div class="highlight"><pre><span></span><code><span class="c1"># Install Ansible.</span>
pip install ansible<span class="o">[</span>azure<span class="o">]</span>
</code></pre></div>

<p>安装ansible-galaxy，这一步应该不是必要的：</p>
<div class="highlight"><pre><span></span><code><span class="c1"># Install Ansible modules and plugins for interacting with Azure.</span>
ansible-galaxy collection install azure.azcollection
</code></pre></div>

<p>实际用下来，不安装也不影响使用，不过可以看下安装的文件。  </p>
<p>安装其他依赖，这一步<strong>很重要</strong>。具体要安装的包，直接按官方提供的来就好：</p>
<div class="highlight"><pre><span></span><code><span class="c1"># Install required modules for Ansible on Azure</span>
wget https://raw.githubusercontent.com/ansible-collections/azure/dev/requirements-azure.txt

<span class="c1"># Install Ansible modules</span>
pip install -r requirements-azure.txt
</code></pre></div>

<p>很多azure的包，并且都指定了版本，否则会有各种冲突。所以需要用官方提供的这个设置了安装。  </p>
<h4 id="azure-ping-ju"><a class="toclink" href="#azure-ping-ju">Azure 凭据</a></h4>
<p>有两种认证方式：配置文件或环境变量。  </p>
<h5 id="pei-zhi-wen-jian"><a class="toclink" href="#pei-zhi-wen-jian">配置文件</a></h5>
<p>配置文件要放在指定位置：<code>~/.azure/credentials</code>。<br>
文件路径是写死的，不能改。如果需要在别的位置，可以通过软连接：</p>
<div class="highlight"><pre><span></span><code>ln -s /workspaces/.azure ~/.azure
</code></pre></div>

<p>这是个ini的配置文件：</p>
<div class="highlight"><pre><span></span><code><span class="k">[default]</span><span class="w"></span>
<span class="na">subscription_id</span><span class="o">=</span><span class="s">&lt;your-subscription_id&gt;</span><span class="w"></span>
<span class="na">client_id</span><span class="o">=</span><span class="s">&lt;security-principal-appid&gt;</span><span class="w"></span>
<span class="na">secret</span><span class="o">=</span><span class="s">&lt;security-principal-password&gt;</span><span class="w"></span>
<span class="na">tenant</span><span class="o">=</span><span class="s">&lt;security-principal-tenant&gt;</span><span class="w"></span>
</code></pre></div>

<p>可以创建多个账号，每个账号使用一个用方括号包围起来的[section]。使用的时候，默认都是[default]。不过可以通过profile参数来指定。</p>
<h5 id="huan-jing-bian-liang"><a class="toclink" href="#huan-jing-bian-liang">环境变量</a></h5>
<p>环境变量用起来更方便：</p>
<div class="highlight"><pre><span></span><code><span class="nb">export</span> <span class="nv">AZURE_SUBSCRIPTION_ID</span><span class="o">=</span>&lt;your-subscription_id&gt;
<span class="nb">export</span> <span class="nv">AZURE_CLIENT_ID</span><span class="o">=</span>&lt;security-principal-appid&gt;
<span class="nb">export</span> <span class="nv">AZURE_SECRET</span><span class="o">=</span>&lt;security-principal-password&gt;
<span class="nb">export</span> <span class="nv">AZURE_TENANT</span><span class="o">=</span>&lt;security-principal-tenant&gt;
</code></pre></div>

<h4 id="shi-li-chuang-jian-zi-yuan-zu"><a class="toclink" href="#shi-li-chuang-jian-zi-yuan-zu">示例：创建资源组</a></h4>
<p>使用ansible创建资源组的示例：</p>
<div class="highlight"><pre><span></span><code><span class="o">---</span><span class="w"></span>
<span class="o">-</span><span class="w"> </span><span class="n">hosts</span><span class="p">:</span><span class="w"> </span><span class="n">localhost</span><span class="w"></span>
<span class="w">  </span><span class="n">connection</span><span class="p">:</span><span class="w"> </span><span class="n">local</span><span class="w"></span>
<span class="w">  </span><span class="n">tasks</span><span class="p">:</span><span class="w"></span>
<span class="w">    </span><span class="o">-</span><span class="w"> </span><span class="n">name</span><span class="p">:</span><span class="w"> </span><span class="n">Creating</span><span class="w"> </span><span class="n">resource</span><span class="w"> </span><span class="n">group</span><span class="w"> </span><span class="o">-</span><span class="w"> </span><span class="s2">&quot;{{ name }}&quot;</span><span class="w"></span>
<span class="w">      </span><span class="n">azure_rm_resourcegroup</span><span class="p">:</span><span class="w"></span>
<span class="w">        </span><span class="n">name</span><span class="p">:</span><span class="w"> </span><span class="s2">&quot;{{ name }}&quot;</span><span class="w"></span>
<span class="w">        </span><span class="n">location</span><span class="p">:</span><span class="w"> </span><span class="s2">&quot;{{ location }}&quot;</span><span class="w"></span>
<span class="w">      </span><span class="n">register</span><span class="p">:</span><span class="w"> </span><span class="n">rg</span><span class="w"></span>
<span class="w">    </span><span class="o">-</span><span class="w"> </span><span class="n">debug</span><span class="p">:</span><span class="w"></span>
<span class="w">        </span><span class="k">var</span><span class="p">:</span><span class="w"> </span><span class="n">rg</span><span class="w"></span>
<span class="o">...</span><span class="w"></span>
</code></pre></div>

<p>主机都是选择localhost。  </p>
<p>使用ansible-playbook运行：</p>
<div class="highlight"><pre><span></span><code>ansible-playbook create_rg.yml --extra-vars <span class="s2">&quot;name=&lt;resource_group_name&gt; location=&lt;resource_group_location&gt;&quot;</span>
</code></pre></div>
      </div>
      <div class="back-to-top">
        <a href="/">HOME</a>
        <a href="#top">TOP</a>
      </div>
    </article>
  </div>
<!-- end article -->
<!-- 页面往下滚动一段之后才会显示TOC -->
<script>
  window.onscroll = function() {
    var tocbox = document.getElementsByClassName('toc')[0];
    var osTop = document.documentElement.scrollTop || document.body.scrollTop;
    var osWidth = document.documentElement.scrollWidth || document.body.scrollWidth;
    // console.log(osTop)
    if (osTop>300 && osWidth>865) {
      tocbox.style.display = "block"
    }
    if (osTop<300 || osWidth<865) {
      tocbox.style.display = "none"
    }
  }
</script>
                <footer>
                    <div class="icons">
                    </div>
                    <span id="busuanzi_container_page_pv" style="padding: 10px">本文阅读量<span id="busuanzi_value_page_pv"></span>次</span>
                    <span id="busuanzi_container_site_pv" style="padding: 10px">本站总访问量<span id="busuanzi_value_site_pv"></span>次</span>
                    <span id="busuanzi_container_site_uv" style="padding: 10px">本站总访客数<span id="busuanzi_value_site_uv"></span>人</span>
                    <p>© <script>document.write(moment().format('YYYY'));</script> 749B</p>
                </footer>
        </div>
</body>
</html>